﻿using Caliburn.Micro;
using MusicExplorer.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using MusicExplorer.Extensions;

namespace MusicExplorer.ViewModels.Wrappers
{
    public class EntityResultViewModel : PropertyChangedBase
    {
        private readonly GroupEditionViewModel m_parent;
        private readonly Entity m_entity;

        protected EntityResultViewModel(GroupEditionViewModel parent, Entity entity, int index)
        {
            m_entity = entity;
            m_parent = parent;
            m_index = index;
        }

        public static async Task<EntityResultViewModel> CreateAsync(GroupEditionViewModel parent, Entity entity, int index)
        {
            EntityResultViewModel wrapper = null;
            switch(entity.Type)
            {
                case EntityType.Youtube:
                    wrapper = new YoutubeEntityResultViewModel(parent, (Youtube)entity, index);
                    break;
                default:
                    wrapper = new EntityResultViewModel(parent, entity, index);
                    break;
            }
            await wrapper.InitializeAsync();
            return wrapper;
        }

        public async Task InitializeAsync()
        {
            Windows.UI.Core.CoreWindow window = Windows.UI.Core.CoreWindow.GetForCurrentThread();
            m_repositoryIcon = new BitmapImage();
            await m_repositoryIcon.FillWithIconRepositoryAsync(m_entity.Repository);
            NotifyOfPropertyChange(() => RepositoryIcon);
        }

        private BitmapImage m_repositoryIcon;
        public BitmapImage RepositoryIcon { get { return m_repositoryIcon; } }

        public GroupEditionViewModel Parent { get { return m_parent; } }
        public Entity Entity { get { return m_entity; } }
        public string DisplayName { get { return m_entity.Name; } }
        public string RepositoryName { get { return m_entity.Repository.Name; } }

        private int m_index;
        public int Index
        {
            get { return m_index; }
            set
            {
                if(m_index != value)
                {
                    m_index = value;
                    NotifyOfPropertyChange(() => Index);
                    NotifyOfPropertyChange(() => PrimaryBackground);
                }
            }
        }

        private bool m_isSelected;
        public bool IsSelected
        {
            get { return m_isSelected; }
            set
            {
                if(m_isSelected != value)
                {
                    m_isSelected = value;
                    NotifyOfPropertyChange(() => IsSelected);
                    Parent.RefreshCommandBar();
                    NotifyOfPropertyChange(() => PrimaryForeground);
                    NotifyOfPropertyChange(() => PrimaryBackground);
                    NotifyOfPropertyChange(() => SecondaryForeground);
                }
            }
        }

        /// UI
        /// ..
        public SolidColorBrush PrimaryBackground
        {
            get
            {
                var brush = new SolidColorBrush();
                if (m_isSelected)
                    brush.Color = Color.FromArgb(255, 0x00, 0x77, 0x00);
                else
                {
                    if (m_index % 2 == 0)
                        brush.Color = Colors.Transparent;
                    else
                        brush.Color = Color.FromArgb(255, 0xE2, 0xE2, 0xE2);
                }
                return brush;
            }
        }

        public SolidColorBrush PrimaryForeground
        {
            get
            {
                var brush = new SolidColorBrush();
                if (m_isSelected)
                    brush.Color = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
                else
                    brush.Color = Color.FromArgb(255, 0x22, 0x22, 0x22);
                return brush;
            }
        }

        public SolidColorBrush SecondaryForeground
        {
            get
            {
                var brush = new SolidColorBrush();
                if (m_isSelected)
                    brush.Color = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
                else
                    brush.Color = Color.FromArgb(255, 0x80, 0x80, 0x80);
                return brush;
            }
        }
    }
}
